package com.sdruixinggroup.mondayb2b;

import android.app.Application;
import android.content.Context;
import android.view.WindowManager;

import com.sdruixinggroup.mondayb2b.API.Constants;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.concurrent.TimeUnit;

import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import okhttp3.OkHttpClient;


/**
 * Created by jia on 17/11/8.
 */

public class B2bApplication extends Application {

    public static B2bApplication instance;
    private static IWXAPI api;
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        initScreenSize();
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30000L, TimeUnit.MILLISECONDS)
                .readTimeout(30000L, TimeUnit.MILLISECONDS)
                .build();

        OkHttpUtils.initClient(okHttpClient);

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, true);
        //oauth = DiffDevOAuthFactory.getDiffDevOAuth();

        JPushInterface.setDebugMode(BuildConfig.DEBUG);
        JPushInterface.init(getApplicationContext());
        CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(this,R.layout.view_jpush,R.id.jpush_icon,R.id.jpush_title,R.id.jpush_content);
        builder.statusBarDrawable = R.mipmap.nongzi;
        builder.layoutIconDrawable = R.mipmap.nongzi;
        JPushInterface.setDefaultPushNotificationBuilder(builder);
    }



    /**
     * 初始化屏幕宽高，若失败，取默认的
     */
    private void initScreenSize() {
        WindowManager wm = (WindowManager) getSystemService(
                Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        if (width > 0) {
            RunningContext.screenWidth = width;
        } else {
            RunningContext.screenWidth = (int) getResources()
                    .getDimension(R.dimen.default_screen_width);
        }
        if (height > 0) {
            RunningContext.screenHeight = height;
        } else {
            RunningContext.screenHeight = (int) getResources()
                    .getDimension(R.dimen.default_screen_height);
        }
    }

    public static IWXAPI getApi() {
        if(null == api){
            api = WXAPIFactory.createWXAPI(instance, Constants.APP_ID, true);
        }
        return api;
    }
}
